package com.snail.billing.fragment.view;

import com.android.util.ResUtil;
import com.snail.billing.Resource;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class FloatView extends ImageView {

	private float x;
	private float y;
	private float mTouchStartX;
	private float mTouchStartY;

	private WindowManager wm;
	private WindowManager.LayoutParams wmParams;

	private int screenWidth;
	private int screenHeight;
	private int topHeight;

	private boolean isShow;
	
	private float startX;
	private float startY;
	
	private boolean isLight;
	private Handler handler;
	private Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			FloatView.super.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_float_icon));
			isLight = false;
		}
	};

	public FloatView(Context context) {
		super(context);
		wm = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		wmParams = new WindowManager.LayoutParams();
		wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 系统提示类型,重要
		wmParams.format = 1;
		wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 不能抢占聚焦点
		wmParams.flags = wmParams.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
		wmParams.flags = wmParams.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; // 排版不受限制

		wmParams.alpha = 1.0f;

		wmParams.gravity = Gravity.LEFT | Gravity.BOTTOM; // 调整悬浮窗口至左上角
		wmParams.x = 0;
		wmParams.y = 0;

		screenWidth = getResources().getDisplayMetrics().widthPixels;
		screenHeight = getResources().getDisplayMetrics().heightPixels;

		setTopHeight(((Activity) getContext()).getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop());
		setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_float_icon));
	}

	public void show() {
		if (isShow) {
			return;
		}
		wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		wm.addView(this, wmParams);
		isShow = true;
		handler = new Handler(Looper.getMainLooper());
		handler.postDelayed(runnable, 5000);
	}

	public void dismiss() {
		if (!isShow) {
			return;
		}
		wm.removeView(this);
		isShow = false;
		handler.removeCallbacks(runnable);
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
		// 设置悬浮窗口长宽数据
		wmParams.width = bitmap.getWidth();
		wmParams.height = bitmap.getHeight();
	}

	public void setTopHeight(int topHeight) {
		this.topHeight = topHeight;
	}

	public WindowManager.LayoutParams getWmParams() {
		return wmParams;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		x = event.getRawX();
		y = event.getRawY() - topHeight;
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				handler.removeCallbacks(runnable);
				if (!isLight) {
					super.setImageResource(ResUtil.getDrawableId(Resource.drawable.snailbilling_float_light_icon));
					isLight = true;
				}
				mTouchStartX = event.getX();
				mTouchStartY = event.getY();
				startX = x;
				startY = y;
			break;
			case MotionEvent.ACTION_MOVE:
				updateViewPosition();
			break;
			case MotionEvent.ACTION_UP:
				updateViewPosition();
				mTouchStartX = 0;
				mTouchStartY = 0;
				float dx = x - startX;
				float dy = y - startY;
				double delta = Math.sqrt(dx * dx + dy * dy);
				System.out.println("x=" + x + ";y=" + y);
				System.out.println("startX=" + startX + ";startY=" + startY);
				System.out.println("dx=" + dx + ";dy=" + dy);
				System.out.println("delta=" + delta );
				handler.postDelayed(runnable, 5000);
				if (delta > 5) {
					return true;
				}
			break;
		}
		return super.onTouchEvent(event);
	}

	private void updateViewPosition() {
		// 更新浮动窗口位置参数
		int newX = (int) (x - mTouchStartX);
		int newY = (int) (y - mTouchStartY);
		int maxX = screenWidth - getWidth();
		int maxY = screenHeight - getHeight() - topHeight;
		if (newX < 0) {
			newX = 0;
		}
		if (newX > maxX) {
			newX = maxX;
		}
		if (newY < 0) {
			newY = 0;
		}
		if (newY > maxY) {
			newY = maxY;
		}
		wmParams.x = newX;
		wmParams.y = maxY - newY;
		wm.updateViewLayout(this, wmParams);
	}

}
